#include "header.h"

static char line[80], value[80], param[80];
static unsigned int j = 0;

void read_input_parameters(input_ptr params) {
 FILE* fid = fopen(params->runname,"r");
 FILE* fhlog = fopen("run.log","w");

 if (fid == NULL) {
  printf("Input file missing\n");
  exit(1);
 }
 while (fgets(line, 80, fid)!=NULL) {
  sscanf(line, "%s\t%s", param, value); 
  if (strcmp(param,"#grids") == 0)    params->n_grids = atoi(value);
  if (strcmp(param,"#t_max") == 0)    params->t_max   = atof(value);
  if (strcmp(param,"#t_cfl") == 0)    params->cfl     = atof(value);
  if (strcmp(param,"#h_step") == 0)   params->h       = atof(value);
  if (strcmp(param,"#mr_tol") == 0)   params->mr_tol  = atof(value);
  if (strcmp(param,"#ic_type") == 0)  params->ic_type = atoi(value);
  if (strcmp(param,"#dims") == 0)     params->dims    = atoi(value);
  if (strcmp(param,"#zakh") == 0)     params->zakh    = atoi(value);
  if (strcmp(param,"#amplit") == 0)   params->a       = atof(value);
  if (strcmp(param,"#sigma") == 0)    params->sigma   = atof(value);
  if (strcmp(param,"#out_skip") == 0) params->out_skip = atoi(value);
  if (strcmp(param,"#hyp_v") == 0)    params->hyp_v    = atof(value);
  if (strcmp(param,"#tau_skip") == 0) params->tau_skip = atof(value);
  if (strcmp(param,"#res_name") == 0) strcpy(params->res_name, value);
  if (strcmp(param,"#grid_str") == 0) {
    for (j = 0; j < (params->n_grids); j++) {
      if (fgets(line, 80, fid)==NULL) {
        printf("Unexpected EOF\n");
        exit(1);
      }
      sscanf(line, "%s\t%s", param, value); 
      if (strcmp(param,"#") == 0)  params->n_points[j] = atoi(value);
    }
  }
 }
 fprintf(fhlog, "#n_grids\t%4d\n", params->n_grids);
 fprintf(fhlog, "#t_max  \t%4f\n", params->t_max);
 fprintf(fhlog, "#zakh   \t%4d\n", params->zakh);
 fprintf(fhlog, "#h_step \t%4f\n", params->h);
 fprintf(fhlog, "#t_cfl  \t%4f\n", params->cfl);
 fprintf(fhlog, "#dims   \t%4d\n", params->dims);
 fprintf(fhlog, "#amplit \t%4f\n", params->a);
 fprintf(fhlog, "#sigma  \t%4f\n", params->sigma);
 for (j = 0; j < params->n_grids; j++) {
  fprintf(fhlog, "#%2d\t%5d\n", j, params->n_points[j]);
 }
 fprintf(fhlog, "#ic_type\t%d\n", params->ic_type);
 fprintf(fhlog, "#restart file name = %s\n", params->res_name);
 fclose(fhlog); 
 fclose(fid);
}





